package com.shishi.fast.generator.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shishi.fast.common.util.HttpClientHelper;
import com.shishi.fast.generator.domain.CollectDataDO;
import com.shishi.fast.generator.domain.DeviceDO;
import com.shishi.fast.generator.mapper.CollectDataMapper;
import com.shishi.fast.generator.mapper.DeviceMapper;
import com.shishi.fast.generator.service.CollectDataService;
import com.shishi.fast.generator.vo.AddDeviceInfoParam;
import com.shishi.fast.generator.vo.CollectDataRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class CollectDataServiceImpl extends ServiceImpl<CollectDataMapper, CollectDataDO> implements CollectDataService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private CollectDataMapper collectDataMapper;

    @Override
    public void makeTodayData(CollectDataRequest.MakeTodayDataParam param) {
        //宿迁
//        String collectDataUrl = "http://106.15.126.19:8051/shishi-fast-sq/collectData/deviceInfoList/save";
        //宿迁-i
        String collectDataUrl = "http://39.106.138.190:8031/shishi-fast-sq-i/collectData/deviceInfoList/save";
        //众山
//        String collectDataUrl = "http://146.56.240.19:8070/shishi-fast-erp/collectData/deviceInfoList/save";

        List<DeviceDO> deviceDOList = deviceMapper.selectList(Wrappers.<DeviceDO>lambdaQuery()
                .eq(DeviceDO::getCompanyId, param.getCompanyId())
                .eq(DeviceDO::getDeleted, 0)
        );

        if (ObjectUtils.isEmpty(deviceDOList)) {
            return;
        }

        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 设置早上八点的时间
        LocalDateTime eightAM = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 8, 0);

        // 计算时间差，转换为分钟
        long minutesDiff = ChronoUnit.MINUTES.between(eightAM, now);

        // 循环次数为时间差除以5（向上取整），因为每次循环代表五分钟
        int loopCount = (int) Math.ceil((double) minutesDiff / 5);

        // 进行循环
        for (int j = 0; j < loopCount; j++) {
            // 这里可以放置你想要在每个五分钟循环中执行的代码
            System.out.println("Loop " + (j + 1) + ": " + eightAM.plusMinutes(j * 5));

            LocalDateTime localDateTime = eightAM.plusMinutes(j * 5);

            // 定义日期时间格式（例如：yyyy-MM-dd HH:mm:ss）
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            // 将LocalDateTime转换为日期字符串
            String dateString = localDateTime.format(formatter);

            //最终上传的数据集合
            List<AddDeviceInfoParam> addDeviceInfoParamList = new ArrayList<>();

            //循环设备发送数据
            for (DeviceDO deviceDO : deviceDOList) {
                AddDeviceInfoParam addDeviceInfoParam = new AddDeviceInfoParam();
                addDeviceInfoParam.setDeviceStatus(2);
                addDeviceInfoParam.setCompanyId(param.getCompanyId());
                addDeviceInfoParam.setDeviceNo(deviceDO.getDeviceNo());
                addDeviceInfoParam.setSources("S100");
                addDeviceInfoParam.setCyclesTimes(j * 300L);
                addDeviceInfoParam.setPowerSecond(j * 300L);
                addDeviceInfoParam.setStandbySecond(0L);
                addDeviceInfoParam.setWorkSecond(j * 300L);
                addDeviceInfoParam.setAlarmSecond(0L);
                addDeviceInfoParam.setSignalFailSecond(0L);

                addDeviceInfoParam.setCreateTime(dateString);

                addDeviceInfoParamList.add(addDeviceInfoParam);
            }

            String httpPost = HttpClientHelper.sendJsonHttpPost(collectDataUrl, JSONObject.toJSONString(addDeviceInfoParamList));
        }

    }

    @Override
    public void deleteTodayData(CollectDataRequest.MakeTodayDataParam param) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = simpleDateFormat.format(System.currentTimeMillis());

        collectDataMapper.delete(Wrappers.<CollectDataDO>lambdaQuery()
                .eq(CollectDataDO::getCompanyId, param.getCompanyId())
                .likeRight(CollectDataDO::getCreateTime, dateStr)
        );
    }

}




